package com.hnqcgczy.shop.db;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.SQLException;

public class MyTransaction {
    private Connection conn;
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    public MyTransaction(boolean isAutoCommit) {
       // DB mydb = new Mydb();
        try {
            //conn=mydb.getconnectionByMysql();
           conn = comboPooledDataSource.getConnection();
            conn.setAutoCommit(isAutoCommit);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public Connection getConn()throws SQLException{
        return conn;
    }
    public  void closeconn() throws  SQLException{
        comboPooledDataSource.close();
    }
    public void commit() {
            try {
                conn.commit();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    public void rollback() {
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        MyTransaction myTransaction = new MyTransaction(true);
        try {
            System.out.println(myTransaction.getConn().isClosed());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
